﻿/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
#region Using directives

using System;
using System.Collections;
using System.Text;

#endregion

namespace ELE.EnterpriseLibrary.Naming
{
  /// <summary>
  /// A table that holds context objects (INamingContext)
  /// </summary>
	public class ContextTable: DictionaryBase  
  {
    /// <summary>
    /// sets and gets a INamingContext by name
    /// </summary>
    public INamingContext this[ String key ]  
    {
      get  
      {
        return Dictionary[key] != null ? ((INamingContext) Dictionary[key]) : null;
      }
      set  
      {
        Dictionary[key] = value;
      }
    }
    /// <summary>
    /// Return the keys
    /// </summary>
    public ICollection Keys  
    {
      get  
      {
        return( Dictionary.Keys );
      }
    }
    /// <summary>
    /// gets the values
    /// </summary>
    public ICollection Values  
    {
      get  
      {
        return( Dictionary.Values );
      }
    }
    /// <summary>
    /// Add a new INamingContext
    /// </summary>
    /// <param name="key">the name</param>
    /// <param name="value">the INamingContext</param>
    public void Add( String key, INamingContext value )  
    {
      Dictionary.Add( key, value );
    }
    /// <summary>
    /// Check for a key present
    /// </summary>
    /// <param name="key">the name </param>
    /// <returns>the INamingContext</returns>
    public bool Contains( String key )  
    {
      return( Dictionary.Contains( key ) );
    }
    /// <summary>
    /// Remove a INamingContext
    /// </summary>
    /// <param name="key">the name</param>
    public void Remove( String key )  
    {
      Dictionary.Remove( key );
    }
    /// <summary>
    /// Validation method
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected override void OnInsert( Object key, Object value )  
    {
      if ( key.GetType() != Type.GetType("System.String") )
        throw new ArgumentException( "key must be of type String.", "key" );

      if ( !(value is INamingContext ))
        throw new ArgumentException( "value must be of type ELE.EnterpriseLibrary.Naming.INamingContext.", "value" );
    }
    /// <summary>
    /// Validation method
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected override void OnRemove( Object key, Object value )  
    {
      if ( key.GetType() != Type.GetType("System.String") )
        throw new ArgumentException( "key must be of type String.", "key" );
    }
    /// <summary>
    /// Validation method
    /// </summary>
    /// <param name="key"></param>
    /// <param name="oldValue"></param>
    /// <param name="newValue"></param>
    protected override void OnSet( Object key, Object oldValue, Object newValue )  
    {
      if ( key.GetType() != Type.GetType("System.String") )
        throw new ArgumentException( "key must be of type String.", "key" );

      if ( !(newValue is INamingContext ))
        throw new ArgumentException( "newValue must be of type ELE.EnterpriseLibrary.Naming.INamingContext.", "newValue" );
    }
    /// <summary>
    /// Validation method
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected override void OnValidate( Object key, Object value )  
    {
      if ( key.GetType() != Type.GetType("System.String") )
        throw new ArgumentException( "key must be of type String.", "key" );

      if ( !(value is INamingContext) )
        throw new ArgumentException( "value must be of type ctl.Framework.Naming.INamingContext.", "value" );
    }

  }
}
